﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using System.IO;
using System.Diagnostics;
using Tunnel_Cross_Section.相关类;
using Tunnel_Cross_Section.option_frm;

namespace Tunnel_Cross_Section.menue_frm
{
    public partial class open_project_frm : Office2007Form
    {
        DataBase m_db;
        Mfrm mfrm;
        public open_project_frm(Mfrm frm,DataBase db)
        {
            InitializeComponent();
            m_db = db;
            mfrm = frm;
            load_exist_prj();
        }
        //新建区间
        private void btn_new_project_Click(object sender, EventArgs e)
        {
            menue_frm.new_prj_from new_frm = new new_prj_from(m_db, mfrm);
            new_frm.ShowDialog();
            load_exist_prj();
            this.Close();
        }

        //列出现有区间列表
        private void load_exist_prj()
        {
            advTree.Nodes.Clear();
            string sql = "select [线路] from [projects] group by [线路]";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node line_node = new Node();
                line_node.Text = dt.Rows[i][0].ToString();
                sql = "select [区间] from [projects] where [线路]='" + line_node.Text + "' and [单圆]=" + 1;
                DataTable dt_section = m_db.GetDataSet(sql).Tables[0];
                for (int j = 0; j < dt_section.Rows.Count; j++)
                {
                    Node section_node = new Node();
                    section_node.Text = dt_section.Rows[j][0].ToString();
                    line_node.Nodes.Add(section_node);
                }
                line_node.Expanded = true;
                advTree.Nodes.Add(line_node);
            }
        }

        private void btn_no_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            Node n = advTree.SelectedNode;
            if (n==null || n.Level == 0)
            {
                option_frm.message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("请选中了区间再确定!", "错误", 2);
                message.buttonX1.Visible = false;
                message.ShowDialog();
                message.Close();
                return;
            }
            string line = n.Parent.Text;
            string section = n.Text;
            Tunnel_Cross_Section.相关类.Projects prj = new Tunnel_Cross_Section.相关类.Projects(line, section);
            p = prj;
            if (mfrm.current_projects.FindIndex(new Predicate<Projects>(find)) >= 0)
            {
                message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("该区间已打开!", "错误", 2);
                message.buttonX1.Visible = false;
                message.ShowDialog();
                message.Close();
                return;
            }
            mfrm.current_projects.Add(prj);
            mfrm.update_projects();
            mfrm.cur_prj = prj;
            mfrm.Label.Text = "已打开区间: " + prj.section_name;
            
        }

        //寻找重复项,避免重添加
        Projects p;
        bool find(Projects prj)
        {
            if (prj.line_name == p.line_name && prj.section_name == p.section_name)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        #region 重命名区间
        //重命名区间
        private void btn_rename_Click(object sender, EventArgs e)
        {
            Node n = advTree.SelectedNode;
            if (n == null || n.Level == 0)
            {
                option_frm.message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("请选中了区间再确定!", "错误", 3);
                message.buttonX1.Visible = false;
                message.ShowDialog();
                message.Close();
                return;
            }
            n.BeginEdit();
            
        }

        private void advTree_AfterCellEditComplete(object sender, CellEditEventArgs e)
        {
            
            string new_section = e.Cell.Text;
            if (old_section_name != new_section)
            {
                string sql = "update [projects] set [区间]='" + new_section + "' where [区间]='" + old_section_name + "'";
                m_db.ExecuteSQL(sql);
            }
            mfrm.Label.Text = "重命名成功";

        }

        private void advTree_AfterCellEdit(object sender, CellEditEventArgs e)
        {
          
        }
        string old_section_name;
        private void advTree_BeforeCellEdit(object sender, CellEditEventArgs e)
        {
            old_section_name = e.Cell.Text;
        }
        #endregion


        //删除区间
        private void btn_delete_Click(object sender, EventArgs e)
        {
            //message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("暂时不能删除!", "错误", 0);
            //message.ShowDialog();
            //message.Close();
            //return;
            Node n = advTree.SelectedNode;
            if (n == null || n.Level == 0)
            {
                option_frm.message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("请选中了区间再确定!", "错误", 2);
                message.buttonX1.Visible = false;
                message.ShowDialog();
                message.Close();
                return;
            }

            string section_name = n.Text;
            //如果该区间已经被打开
            for (int i = 0; i < mfrm.current_projects.Count; i++)
            {
                if (mfrm.current_projects[i].section_name == section_name)
                {
                    option_frm.message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("该区间正在使用\r\n请关闭后重试!", "错误", 2);
                    message.ShowDialog();
                    message.Close();
                    return;
                }
            }
            message_frm frm = new message_frm("将删除该区间的所有信息?", "确定", 1);
            frm.ShowDialog();
            if (frm.yesorno != true)
            {
                frm.Close();
                return;
            }
            frm.Close();
            
            string sql = "Select [期数],[id] from [projects] where [区间]='" + section_name + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            sql = "Delete from [projects] where [区间]='" + section_name + "'";
            m_db.ExecuteSQL(sql);
            string all_ids = dt.Rows[0][0].ToString();
            string id = (dt.Rows[0][1]).ToString();
            if (all_ids.Length == 0)
            {
                return;
            }
            string[] ids = dt.Rows[0][0].ToString().Split(',');
            string table_n;
            table_n = "qj_" + id;
            m_db.DeleteTable(table_n);
            for (int i = 1; i < ids.Length; i++)
            {
                table_n = "qj_" + id + "_" + ids[i];
                m_db.DeleteTable(table_n);
                table_n = "qj_" + id + "_" + ids[i] + "_data";
                m_db.DeleteTable(table_n);
            }
            DirectoryInfo de = new DirectoryInfo(Application.StartupPath + @"\Logs");
            FileInfo[] infos = de.GetFiles("*.txt");
            for (int i = 0; i < infos.Length; i++)
            {
                string pth_txt = infos[i].FullName;
                if (pth_txt.Contains(section_name))
                {
                    File.Delete(pth_txt);
                }
            }
            //重新加载区间列表
            load_exist_prj();
            mfrm.Label.Text = "已永久删除区间: " + section_name;
        }

        //双击添加区间
        private void advTree_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {

            Node n = e.Node;
            if (n == null || n.Level == 0)
            {
                option_frm.message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("请选中了区间再确定!", "错误", 2);
                message.buttonX1.Visible = false;
                message.ShowDialog();
                message.Close();
                return;
            }
            string line = n.Parent.Text;
            string section = n.Text;
            Tunnel_Cross_Section.相关类.Projects prj = new Tunnel_Cross_Section.相关类.Projects(line, section);
            p = prj;
            if (mfrm.current_projects.FindIndex(new Predicate<Projects>(find)) >= 0)
            {
                message_frm message = new Tunnel_Cross_Section.option_frm.message_frm("该区间已打开!", "错误", 2);
                message.buttonX1.Visible = false;
                message.ShowDialog();
                message.Close();
                return;
            }
            mfrm.current_projects.Add(prj);
            mfrm.update_projects();
            mfrm.Label.Text = "已打开区间: " + prj.section_name;
            mfrm.cur_prj = prj;
            this.Close();
        }

    }
}